package secondkill.rabbtmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Package: com.seckillpro.seckill_pro.rabbtmq
 * @ClassName: MQSender
 * @Author: lyx
 * @CreateTime: 2021/3/26 17:13
 * @Description:
 */
@Slf4j
@Service
public class MQSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

/*
    //Fanout模式
    public void send_Fanout(Object msg){
        log.info("Fanout_Exchange发送消息： " + msg);
        rabbitTemplate.convertAndSend("Fanout_Exchange","",msg);
    }

    //Direct模式
    public void send_Direct01(Object msg){
        log.info("Direct_Exchange发送消息： " + msg);
        rabbitTemplate.convertAndSend("Direct_Exchange","Direct_Key01",msg);
    }
    public void send_Direct02(Object msg){
        log.info("Direct_Exchange发送消息： " + msg);
        rabbitTemplate.convertAndSend("Direct_Exchange","Direct_Key02",msg);
    }

    //Topic模式
    public void send_Topic01(Object msg){
        log.info("Topic_Exchange发送消息： " + msg);
        rabbitTemplate.convertAndSend("Topic_Exchange","key.will.go.first.exchange",msg);
    }
    public void send_Topic02(Object msg){
        log.info("Topic_Exchange发送消息： " + msg);
        rabbitTemplate.convertAndSend("Topic_Exchange","both.key.will.go.first.exchange",msg);
    }
    //Headers模式
    public void send_Headers01(String msg){
        log.info("Headers_Exchange发送消息(只被一个接收)： " + msg);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color","red");
        Message message = new Message(msg.getBytes(),properties);
        rabbitTemplate.convertAndSend("Headers_Exchange","",message);
    }
    public void send_Headers02(String msg){
        log.info("Headers_Exchange发送消息(两个接收)： " + msg);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color","red");
        properties.setHeader("speed","fast");
        Message message = new Message(msg.getBytes(),properties);
        rabbitTemplate.convertAndSend("Headers_Exchange","both.key.will.go.first.exchange",message);
    }
*/

    /**
     * 发送秒杀信息
     * @param message
     */
    public void sendSeckillMessage(String message) {
        log.info("发送消息" + message);
        rabbitTemplate.convertAndSend("seckillExchange", "seckill.message", message);
    }
}
